package org.jeecg.modules.demo.yxmodalshift.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.pubuyorder.service.IPuBuyOrderService;
import org.jeecg.modules.demo.soOrderDetail.entity.SoOrderDetail;
import org.jeecg.modules.demo.soOrderDetail.service.ISoOrderDetailService;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.warehouseorder.entity.WarehouseOrder;
import org.jeecg.modules.demo.warehouseorder.service.IWarehouseOrderService;
import org.jeecg.modules.demo.yxmodalshift.entity.YxModalshift;
import org.jeecg.modules.demo.yxmodalshift.service.IYxModalshiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 形态转换单
 * @Author: jeecg-boot
 * @Date:   2022-07-15
 * @Version: V1.0
 */
@Api(tags="形态转换单")
@RestController
@RequestMapping("/yxmodalshift/yxModalshift")
@Slf4j
public class YxModalshiftController extends JeecgController<YxModalshift, IYxModalshiftService> {
	@Autowired
	private IYxModalshiftService yxModalshiftService;
	@Autowired
	private IWarehouseOrderService warehouseOrderService;
	@Autowired
	private IPuBuyOrderService puBuyOrderService;
	 @Autowired
	 private ISoOrderDetailService soOrderDetailService;


	/**
	 * 分页列表查询
	 *
	 * @param yxModalshift
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "形态转换单-分页列表查询")
	@ApiOperation(value="形态转换单-分页列表查询", notes="形态转换单-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="yunxiao/yxmodalshift/YxModalshiftList")
	public Result<?> queryPageList(YxModalshift yxModalshift,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<YxModalshift> queryWrapper = QueryGenerator.initQueryWrapper(yxModalshift, req.getParameterMap());
		Page<YxModalshift> page = new Page<YxModalshift>(pageNo, pageSize);
		IPage<YxModalshift> pageList = yxModalshiftService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param yxModalshift
	 * @return
	 */
	@AutoLog(value = "形态转换单-添加")
	@ApiOperation(value="形态转换单-添加", notes="形态转换单-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody YxModalshift yxModalshift,HttpServletRequest req) {
		try{
			WarehouseOrder warehouseOrder = warehouseOrderService.getByWoNo(yxModalshift.getWoNo());
			warehouseOrder.setZhzt(1);
			String tenantId = req.getHeader("tenant-id");
			String precode = puBuyOrderService.getTeantCodeById(Integer.parseInt(tenantId)).toUpperCase();
			String str = yxModalshiftService.getMaxCode();
			Map<String,String> map = new HashMap<>();
			map.put("maxCode", str==null?null:str);
			String name =precode+ "ZH_";
			String resultCode = generateOddUtil.createCode(map,name);
			yxModalshift.setZhNo(resultCode);
			yxModalshift.setShenhe(0);
			yxModalshift.setCreateTime(new Date());
			warehouseOrderService.updateById(warehouseOrder);
			yxModalshiftService.save(yxModalshift);
			return Result.OK("添加成功！");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}
	 /**
	  *  审核
	  *
	  * @param yxModalshift
	  * @return
	  */
	 @AutoLog(value = "形态转换单-审核")
	 @ApiOperation(value="形态转换单-审核", notes="形态转换单-审核")
	 @PostMapping(value = "/checkModalShift")
	 public Result<?> checkModalShift(@RequestBody YxModalshift yxModalshift,HttpServletRequest req) {
	 	try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			if(!sysUser.getId().equals(yxModalshift.getSaleman())){
				return Result.error(500,"您不是当前单据的指定审核人！");
			}
			if(yxModalshift.getShenhe()==1){
				return Result.error(500,"已审核，无需重复审核！");
			}
			String tenantId = req.getHeader("tenant-id");
			String precode = puBuyOrderService.getTeantCodeById(Integer.parseInt(tenantId)).toUpperCase();
			if(yxModalshiftService.checkModalShift(yxModalshift,precode)){
				return Result.OK("审核成功!");
			}
			return Result.error(500,"审核失败！");
		}catch (Exception e){
	 		e.printStackTrace();
	 		return Result.error(500,"系统错误！");
		}
	 }
	 /**
	  *  弃审
	  *
	  * @param yxModalshift
	  * @return
	  */
	 @AutoLog(value = "形态转换单-弃审")
	 @ApiOperation(value="形态转换单-弃审", notes="形态转换单-弃审")
	 @PostMapping(value = "/unCheckModalShift")
	 public Result<?> unCheckModalShift(@RequestBody YxModalshift yxModalshift,HttpServletRequest req) {
		try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			if(!sysUser.getId().equals(yxModalshift.getSaleman())){
				return Result.error(500,"您不是当前单据的指定审核人！");
			}
			if(yxModalshift.getShenhe()==0){
				return Result.error(500,"请先审核单据！");
			}
			List<SoOrderDetail> soOrderDetails =soOrderDetailService.getByWoNo(yxModalshift.getQtrkno());
			if(!soOrderDetails.isEmpty()){
				return Result.error(300,"该单后续单据已生成销售出库单，无法弃审！");
			}
			WarehouseOrder order = warehouseOrderService.getByRkNo(yxModalshift.getQtrkno());
			if(order.getShenhe()==1){
				return Result.error(300,"该单后续单据已审核，无法弃审！");
			}
			SoOrderDetail orderDetail = soOrderDetailService.getBySodNumber(yxModalshift.getQtckno());
			if(orderDetail.getSodStatus()==1){
				return Result.error(300,"该单后续单据已审核，无法弃审！");
			}
			if(yxModalshiftService.unCheckModalShift(yxModalshift)){
				return Result.OK("弃审成功!");
			}
			return Result.error(500,"弃审失败！");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}

	 }
	/**
	 *  编辑
	 *
	 * @param yxModalshift
	 * @return
	 */
	@AutoLog(value = "形态转换单-编辑")
	@ApiOperation(value="形态转换单-编辑", notes="形态转换单-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody YxModalshift yxModalshift) {
		try{
			yxModalshiftService.updateById(yxModalshift);
			return Result.OK("编辑成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "形态转换单-通过id删除")
	@ApiOperation(value="形态转换单-通过id删除", notes="形态转换单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		YxModalshift yxModalshift = yxModalshiftService.getById(id);
		if(yxModalshift.getShenhe()==1){
			return Result.error(500,"请先弃审单据！");
		}
		WarehouseOrder warehouseOrder = warehouseOrderService.getByWoNo(yxModalshift.getWoNo());
		warehouseOrder.setZhzt(0);
		warehouseOrderService.updateById(warehouseOrder);
		yxModalshiftService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "形态转换单-批量删除")
	@ApiOperation(value="形态转换单-批量删除", notes="形态转换单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yxModalshiftService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "形态转换单-通过id查询")
	@ApiOperation(value="形态转换单-通过id查询", notes="形态转换单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		YxModalshift yxModalshift = yxModalshiftService.getById(id);
		if(yxModalshift==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yxModalshift);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yxModalshift
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxModalshift yxModalshift) {
        return super.exportXls(request, yxModalshift, YxModalshift.class, "形态转换单");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxModalshift.class);
    }

}
